package com.platform.modules.sys.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.common.annotation.SysLog;
import com.platform.common.page.PageUtil;
import com.platform.common.utils.Constant;
import com.platform.common.utils.Query;
import com.platform.common.utils.RestResponse;
import com.platform.common.validator.AbstractAssert;
import com.platform.common.validator.ValidatorUtils;
import com.platform.common.validator.group.AddGroup;
import com.platform.common.validator.group.UpdateGroup;
import com.platform.common.enumstatus.AptitudeEnum;
import com.platform.common.enumstatus.SystemEnum;
import com.platform.modules.sys.entity.SysNoticeEntity;
import com.platform.modules.sys.entity.SysOrgEntity;
import com.platform.modules.sys.entity.SysOrgUserEntity;
import com.platform.modules.sys.entity.SysRoleEntity;
import com.platform.modules.sys.entity.SysUserAptitudeEntity;
import com.platform.modules.sys.entity.SysUserEntity;
import com.platform.modules.sys.form.PasswordForm;
import com.platform.modules.sys.service.SysDictService;
import com.platform.modules.sys.service.SysNoticeService;
import com.platform.modules.sys.service.SysOrgService;
import com.platform.modules.sys.service.SysOrgUserService;
import com.platform.modules.sys.service.SysRoleService;
import com.platform.modules.sys.service.SysUserAptitudeService;
import com.platform.modules.sys.service.SysUserRoleService;
import com.platform.modules.sys.service.SysUserService;

/**
 * 
 * 2019-09-05
 * @author wangLc
 */
@Api(tags = "SysNoticeController|系统通知公告")
@RestController
@RequestMapping("/sys/notice")
public class SysNoticeController extends AbstractController {
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysOrgUserService sysOrgUserService;
	@Autowired
	private SysDictService sysDictService;
	@Autowired
	private SysNoticeService sysNoticeService;
	

	/**
	 * 发布单位查看分页列表
	 * @param params
	 * @return RestResponse
	 */
	@ApiOperation(value = "发布单位查看分页列表", notes = "发布单位查看分页列表")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = "query", name = "currPage", value = "当前页数", dataType = "Integer"),
   	 	@ApiImplicitParam(paramType = "query", name = "limit", value = "分页步数", dataType = "Integer"),
		@ApiImplicitParam(paramType = "query", name = "noticeTitle", value = "通知公告", dataType = "String"),
		@ApiImplicitParam(paramType = "query", name = "acceptOrgType", value = "单位类型", dataType = "String")
	})
	@PostMapping("/queryAll")
	public RestResponse queryAll(Integer currPage, Integer limit,String noticeTitle,String acceptOrgType) {
		
		Map<String, Object> params = new HashMap<String, Object>();
		
			params.put("noticeTitle", noticeTitle);
			params.put("acceptOrgType", acceptOrgType);
			params.put("page", PageUtil.getCurrPage(currPage));
	        params.put("limit", PageUtil.getLimit(limit));
		Page page = sysNoticeService.queryPage(params);
		return RestResponse.success().put("page", page);
	}

	/**
	 * 接受者查看消息  
	 * @param params
	 * @return RestResponse
	 */
	@ApiOperation(value = "接受者查看消息", notes = "接受者查看消息")
	@ApiImplicitParams({
	@ApiImplicitParam(paramType = "query", name = "noticeTitle", value = "通知公告", dataType = "String")
	})
	@PostMapping("/acceptNoticeList")
	public RestResponse acceptNoticeList(String noticeTitle) {
		
		SysUserEntity  user =  getUser();
		String accountType = user.getAccountType();
		String orgType = "";
		if(user != null && user.getUserId() != null){
			SysUserEntity sysUserEntity = sysUserService.getById(user.getUserId());
			if(sysUserEntity != null){
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("userId", sysUserEntity.getUserId());
				List<SysOrgUserEntity> orgUserList = sysOrgUserService.queryAll(params);
				if(orgUserList !=null && orgUserList.size()>0){
					orgType = orgUserList.get(0).getOrgNo();
				}

			}
		}
		
		Map<String, Object> params = new HashMap<String, Object>();
		//专家用户
		if("3".equals(accountType)){
			params.put("acceptOrgType", "9");
		}else {
			params.put("acceptOrgType", orgType);
		}
			params.put("noticeTitle", noticeTitle);
			params.put("showStatus", "ON");
		Page page = sysNoticeService.queryPage(params);
		return RestResponse.success().put("page", page);
	}
	
	
	/**
	 * 查看消息详情  
	 * @param params
	 * @return RestResponse
	 */
	@ApiOperation(value = "查看消息详情", notes = "查看消息详情")
	@ApiImplicitParams({
	@ApiImplicitParam(paramType = "query", name = "noticeId", value = "公告Id", dataType = "String")
	})
	@PostMapping("/showTailByNoticeId")
	public RestResponse showTailByNoticeId(String noticeId) {
		
		if(StringUtils.isBlank(noticeId)){
			return RestResponse.error("请输入noticeId！");
		}
		SysNoticeEntity sysNoticeEntity = sysNoticeService.getById(noticeId);
		if(sysNoticeEntity == null){
			return RestResponse.error("数据错误！");
		}
		return RestResponse.success().put("sysNoticeEntity", sysNoticeEntity);
	}

	/**
	 * 保存新增/修改通知公告
	 * @return RestResponse
	 */
	@ApiOperation(value = "保存新增/修改通知公告", notes = "保存新增/修改通知公告")
	@SysLog("保存新增/修改通知公告")
	@PostMapping("/save")
	public RestResponse save(SysNoticeEntity sysNoticeEntity) {
		
        if(sysNoticeEntity != null){
        	sysNoticeEntity.setCreateTime(new Date());
        	sysNoticeEntity.setCreateUserId(getUserId());
        	if(StringUtils.isNotBlank(sysNoticeEntity.getNoticeId())){
        		sysNoticeService.updateById(sysNoticeEntity);
        	}else{
        		sysNoticeService.save(sysNoticeEntity);
        	}
        }
		return RestResponse.success();
	}

	/**
	 * 保存新增/修改通知公告
	 * @param user
	 * @return RestResponse
	 */
	@ApiOperation(value = "显示/隐藏通知公告", notes = "显示/隐藏通知公告")
	@SysLog("显示/隐藏通知公告")
	@PostMapping("/hideOrshow")
	public RestResponse hideOrshow(String noticeId) {
		if(StringUtils.isBlank(noticeId)){
			return RestResponse.error("请输入noticeId！");
		}
		SysNoticeEntity sysNoticeEntity = sysNoticeService.getById(noticeId);
		
		if(sysNoticeEntity != null){
			if("ON".equals(sysNoticeEntity.getShowStatus())){
				sysNoticeEntity.setShowStatus("OFF");
			}else if("OFF".equals(sysNoticeEntity.getShowStatus())){
				sysNoticeEntity.setShowStatus("ON");
			}
			sysNoticeService.updateById(sysNoticeEntity);
		}else{
			return RestResponse.error("数据有误！");
		}
		return RestResponse.success();
	}

	
	/**
	 * 保存新增/修改通知公告
	 * @return RestResponse
	 */
	@ApiOperation(value = "删除通知公告", notes = "删除通知公告")
	@SysLog("删除通知公告")
	@PostMapping("/delete")
	public RestResponse delete(String noticeId) {
		if(StringUtils.isBlank(noticeId)){
			return RestResponse.error("请输入noticeId！");
		}
		sysNoticeService.removeById(noticeId);
		return RestResponse.success();
	}

	
}
